package com.jdd.common.client;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jdd.common.api.vo.ResultVo;
import com.jdd.common.client.vo.SynObjectVO;
import com.jdd.common.exception.JddException;
import com.jdd.common.util.EncapsulationSignDataUtils;
import com.jdd.common.util.RestTemplateUtils;
import com.jdd.common.util.ResultCode;
import com.jdd.modules.parkcar.entity.ParkSpace;
import com.jdd.modules.parking.entity.ParkCarEnterLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ParkCloudHttpClient {

    public String appScrect;



    public String appId;
    @Resource
    private RestTemplateUtils templateUtils;
    @Resource
    private RestTemplate restTemplate;


    public void synSetParkEnterLogOut(List<SynObjectVO> synObjectVOList){
        log.info("向云端同步的数据信息：{}",synObjectVOList);
        if(CollUtil.isEmpty(synObjectVOList))
        {
            return;
        } //处理头信息与接口信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        JSONObject object = new JSONObject();
        object.put("synObjectVOList",synObjectVOList);
        //封装参数
        MultiValueMap<String, String> stringStringSortedMap = EncapsulationSignDataUtils.encapsulationParameters(object, appId, appScrect);
        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(stringStringSortedMap, headers);
        String url = null;
        //使用restTemplate远程调用云端保存接口
        ResponseEntity<ResultVo> cmsResultDTO = templateUtils.post(url, request, ResultVo.class);
        //云端返回接口保存结果
        ResultVo body = cmsResultDTO.getBody();
        if(null!=body&&ResultCode.SUCCESS==body.getCode())
        {
            log.info("同步滞留车出场信息成功");
            return;
        }else {
            throw new JddException(body.getCode(),body.getMsg());
        }


    }
}
